<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>猜数字游戏</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #e0f7fa;
            display: flex;
            height: 100vh;
            justify-content: center;
            align-items: center;
            flex-direction: column;
        }
        .game-container {
            background-color: #ffffff;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            text-align: center;
        }
        input[type="number"] {
            padding: 10px;
            font-size: 16px;
            width: 200px;
            margin-bottom: 20px;
        }
        button {
            padding: 10px 20px;
            font-size: 16px;
            margin: 5px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }
        button.guess {
            background-color: #4caf50;
            color: white;
        }
        button.reset {
            background-color: #f44336;
            color: white;
        }
        .message {
            margin-top: 20px;
            font-size: 18px;
            height: 24px;
        }
    </style>
</head>
<body>

    <div class="game-container">
        <h1>猜数字游戏</h1>
        <p>请输入一个1到100之间的数字：</p >
        <input type="number" id="guessInput" min="1" max="100">
        <br>
        <button class="guess" id="guessBtn">猜</button>
        <button class="reset" id="resetBtn">重置</button>
        <div class="message" id="message"></div>
    </div>

    <script>
        // 获取元素
        const guessInput = document.getElementById('guessInput');
        const guessBtn = document.getElementById('guessBtn');
        const resetBtn = document.getElementById('resetBtn');
        const messageDiv = document.getElementById('message');

        // 生成随机数
        let targetNumber = generateRandomNumber(1, 100);
        let attempts = 0;

        // 生成随机数函数
        function generateRandomNumber(min, max) {
            return Math.floor(Math.random() * (max - min + 1)) + min;
        }

        // 猜数字函数
        function makeGuess() {
            const guess = parseInt(guessInput.value, 10);
            attempts++;

            if (isNaN(guess)) {
                messageDiv.textContent = '请输入一个有效的数字！';
                return;
            }

            if (guess < 1 || guess > 100) {
                messageDiv.textContent = '请输入一个1到100之间的数字！';
                return;
            }

            if (guess === targetNumber) {
                messageDiv.textContent = `恭喜你，猜对了！数字是 ${targetNumber}。你一共猜了 ${attempts} 次。`;
                guessBtn.disabled = true;
            } else if (guess < targetNumber) {
                messageDiv.textContent = '猜小了！';
            } else {
                messageDiv.textContent = '猜大了！';
            }

            guessInput.value = '';
            guessInput.focus();
        }

        // 重置游戏函数
        function resetGame() {
            targetNumber = generateRandomNumber(1, 100);
            attempts = 0;
            messageDiv.textContent = '';
            guessBtn.disabled = false;
            guessInput.value = '';
            guessInput.focus();
        }

        // 添加事件监听器
        guessBtn.addEventListener('click', makeGuess);
        resetBtn.addEventListener('click', resetGame);

        // 回车键提交
        guessInput.addEventListener('keyup', function(event) {
            if (event.key === 'Enter') {
                makeGuess();
            }
        });
    </script>

</body>
</html>